package com.runing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runing.config.ResponseWrapper;
import com.runing.entity.Admin;
import com.runing.jwt.JWTUtil;
import com.runing.mapper.AdminMapper;
import com.runing.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runing.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-11
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    public Admin findAdminByUsername(String username){
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        return admin;
    }
    public String getPassword(String username){
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        return admin.getPassword();
    }
    public ResponseWrapper login(Admin admin){
        if (admin==null){
            return ResponseWrapper.markParamError();
        }else {
            Admin admins = findAdminByUsername(admin.getUsername());
            if (admins != null && admins.getWorks() == true) {
                String password = Md5Util.md5String(admin.getPassword()+admin.getUsername()+"runing");
                String realPassword = admins.getPassword();
                if (!realPassword.equals(password)) {
                    return ResponseWrapper.markCustom(false, 400, "密码错误", null);
                }

            } else {
                return ResponseWrapper.markCustom(false, 400, "用户不存在或被禁用", null);
            }
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(admins);
            wrapper.getData().put("data",admins);
            wrapper.getData().put("token", JWTUtil.createToken(admin.getUsername()));
            return wrapper;
        }
    }

    public ResponseWrapper findAllAdmins(Integer pageNum,Integer pageSize,String aname){
            QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.like("aname",aname);
            Page<Admin> page = new Page<>(pageNum,pageSize);
            IPage<Admin> iPage =  adminMapper.selectPage(page,adminQueryWrapper);
            List list = iPage.getRecords();
            ResponseWrapper wrapper = new ResponseWrapper().markSuccess(list);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",iPage.getTotal());
            wrapper.getData().put("pageSize",iPage.getSize());
            wrapper.getData().put("pageNum",iPage.getCurrent());
            return wrapper;

    }

    public ResponseWrapper addAdmin(Admin admin){
        if (admin != null){
            QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("username",admin.getUsername());
            int count = adminMapper.selectCount(adminQueryWrapper);
            if (count == 1){
                return ResponseWrapper.markCustom(false,500,"该账号已存在",null);
            }else {
                String passwords = Md5Util.md5String(admin.getPassword()+admin.getUsername()+"runing");
                admin.setPassword(passwords);
                int result = adminMapper.insert(admin);
                if (result == 1){
                    return ResponseWrapper.markSuccess(null);
                }else {
                    return ResponseWrapper.markError();
                }
            }
        }else {
            return ResponseWrapper.markCustom(false,400,"admin为空",null);
        }
    }

    public ResponseWrapper deleteAdmin(String aids){
        if (aids != null){
            //判断ids之间是不是通过-连接
            if (aids.contains("-")){
//                分解ids
                String[] str_ids = aids.split("-");
//                遍历，使分割出的id存入容器
                for (String str_id : str_ids) {
                    System.out.println("str_id =>"+str_id);
                    Integer aid = Integer.parseInt(str_id);
                    adminMapper.deleteById(aid);
                }
                return ResponseWrapper.markCustom(true,200,"删除成功",null);
            } else {
//                单个删除
                Integer id = Integer.parseInt(aids);
                int i = adminMapper.deleteById(id);
                if (i == 1) {
                    return ResponseWrapper.markSuccess(i);
                } else {
                    return ResponseWrapper.markError();
                }
            }
        }else {
            return ResponseWrapper.markCustom(false,500,"aids不能是空",null);
        }
    }

    public ResponseWrapper updateAdmin(Admin admin){
        if (admin == null){
            return ResponseWrapper.markCustom(false,400,"admin不能为空",null);
        }else {
            Admin admins = adminMapper.selectById(admin.getAid());
            if (admin.getPassword() != null){
                String passwords = null;
                if (admin.getPassword().equals("") == false){
                    System.out.println(admin.getPassword());
                    passwords = Md5Util.md5String(admin.getPassword()+admins.getUsername()+"runing");
                }else {
                    Admin sadmin = adminMapper.selectById(admin.getAid());
                    passwords =  sadmin.getPassword();
                }
                admin.setPassword(passwords);
                int result = adminMapper.updateById(admin);
                if (result == 1){
                    return ResponseWrapper.markSuccess(null);
                }else {
                    return ResponseWrapper.markError();
                }
            }else {
                int result = adminMapper.updateById(admin);
                if (result == 1){
                    return ResponseWrapper.markSuccess(null);
                }else {
                    return ResponseWrapper.markError();
                }
            }

        }
    }

    public ResponseWrapper updatePassword(Integer aid, String oldpassword,String newpassword){
        if (aid == null || oldpassword.equals("") || newpassword.equals("")){
            return ResponseWrapper.markCustom(false,400,"aid,oldPassword,newPassword都不能为空",null);
        }else {
            Admin admin = adminMapper.selectById(aid);
            if (admin == null){
                return ResponseWrapper.markCustom(false,500,"用户不存在",null);
            }
            String oldPsw = Md5Util.md5String(oldpassword+admin.getUsername()+"runing");
            if (oldPsw.equals(admin.getPassword())){
                String newPsw = Md5Util.md5String(newpassword+admin.getUsername()+"runing");
                admin.setPassword(newPsw);
                int result = adminMapper.updateById(admin);
                if (result == 1){
                    return ResponseWrapper.markSuccess(result);
                }else {
                    return ResponseWrapper.markError();
                }
            }else {
                return ResponseWrapper.markCustom(false,500,"密码错误",null);
            }
        }
    }

}
